angular.module('myApp')
.factory('TimeService', [function TimeService() {
  return {
    // 生成时间范围数组
    generateRange: function(length) { Array.from({ length }, function(_, i) { return i }) },

    // 获取当前时间对象
    getCurrentTime: function() { 
      return{
        hours: new Date().getHours(),
        minutes: new Date().getMinutes(),
        seconds: new Date().getSeconds()
      }
     },

    // 防抖函数
    debounce: function(fn, delay) {
      let timer;
      return function(...args) {
        clearTimeout(timer);
        timer = setTimeout(function() { fn.apply(this, args) }, delay);
      };
    },

    // 滚动到指定位置
    scrollToCenter: function(element, value) {
      const itemHeight = element.querySelector('li').offsetHeight;
      element.scrollTop = value * itemHeight;
    },

    // 时间格式化
    formatTime: function(time, format) {
      const pad = function(n) { return n.toString().padStart(2, '0') };
      return format
        .replace('HH', pad(time.hours))
        .replace('mm', pad(time.minutes))
        .replace('ss', pad(time.seconds));
    },

    // 解析时间字符串
    parseTime: function(timeStr, format) {
      const parts = {};
      const timeArr = timeStr.split(':');
      format.split(':').forEach(function(part, index) {
        parts[part.toLowerCase()] = parseInt(timeArr[index], 10);
      });
      return parts;
    }
  };
}])
